<template>
	<view class="common-width96 ml-2 common-text-size">
		<view class="list">
			<view class="text-center big-common-text-size mt-2 font-color" v-if="addressList.length == 0">
				<p>您还没有收货地址哦~</p>
			</view>
			<view v-else>
				<view v-for="(item, index) in addressList" :key="index" class="list-item common-text-size" @click="checkAddToUser(index)">
					<view>
						<text>地址：{{item.province}}/{{item.city}}/{{item.area}}/{{item.details}}</text> <br>
						<text>手机号：{{item.phone}}</text> <br>
						<text>名字：{{item.name}}</text>
						<text class="text-red ml-2">{{item.defalut == 1 ? '默认地址' : ''}}</text><br>
					</view>
					<view @click="toEdit(index)">
						<image src="../../../static/edit.png"></image>
					</view>
				</view>
			</view>
		</view>
		<button class="circle-button position-bottom common-width96 mb-2" @click="addAddress">
			<text class="common-text-size">添加地址</text>
		</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: [],
				isChecked: false,
			}
		},
		methods: {
			checkAddToUser(index) {
				let _this = this;
				if (this.isChecked) {
					uni.showActionSheet({
						itemList: ["确定"],
						success(res) {
							let addr = _this.addressList[index];
							let pages = getCurrentPages(); 
							let prevPage = pages[ pages.length - 2 ];
							prevPage.$vm.addr = addr;
							uni.navigateBack()	
						}
					})
				}
			},
			toEdit(index) {
				uni.navigateTo({
					url: `./addaddress/addaddress?form=${JSON.stringify(this.addressList[index])}`
				})
			},
			getAddressList() {
				let _this = this;
				this.$http({
					url: "/address/info/list"
				}).then(response => {
					let data = response.data;
					if (data.data !== undefined) {
						_this.addressList = data.data;
					}
				});
			},
			addAddress() {
				uni.navigateTo({
					url: "addaddress/addaddress"
				})
			}
		},
		onShow() {
			this.getAddressList()
		},
		onLoad(e) {
			if (e.isChecked != null) {
				this.isChecked = true;
			}
		}
	}
</script>

<style>
	@import url("/static/css/common.css");

	.list-item {
		border-bottom: #999999 solid 1px;
		margin: calc(2vh) calc(2vw) calc(2vh) calc(2vw);
		padding-bottom: calc(2vh);
		display: flex;
	}

	.list-item text {
		line-height: calc(3vh);
	}

	.list-item image {
		width: calc(10vw);
		height: calc(10vw);
		margin-top: calc(2vh);
		margin-left: calc(13vw);
	}
</style>
